
mb.dom = {
  scriptContainerElem : (function() {
    return (document.getElementsByTagName('head')[0] || document.body);
  })()
  ,
  createElement : function(config, doc) {
    doc = doc || document;
    config = config || {}
    var el = doc.createElement(config.tagName || 'div');
    jQuery.each(
      mb.lang.keys(config),
      function(index, key) {
        if (key=='tagName') return;
        if (key=='className') el.setAttribute('class', config[key]);
        else el.setAttribute(key, config[key]);
      }
    )
    return el;
  }
  ,
/*  getElementsByClassName : function(el, className, tagName) {
    var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
    jQuery.grep(el.getElementsByTagName(tagName || '*'), function(cn) {
      regexp.test(cn.className);
    })
  }
  ,
  addClass : function(el, className) {
    var regexp = new RegExp('(^|\\s)'+className+'(\\s|$)');
    if (!regexp.test(el.className||'')) {
      el.className = (el.className||'') + ' '+className;
    }
  }
  ,
  removeClass : function(el, className) {
    var regexp = new RegExp('(^|\\s)'+className+'(\\s|$)', 'g');
    el.className = (el.className||'').replace(regexp, ' ');
  }
  ,*/
  toHTML : function(el) {
    if (el.parentNode) el = el.cloneNode(true);
    var ptag = 'div';
    if (el.tagName=='td' || el.tagName=='th') ptag = 'tr';
    else if (el.tagName=='tr') ptag = 'tbody';
    else if (el.tagName=='tbody' || el.tagName=='thead') ptag = 'table';
    else if (el.tagName=='li') ptag = 'ul';
    else if (el.tagName=='option') ptag = 'select';
    var p = mb.dom.createElement({ tagName : ptag });
    p.appendChild(el);
    return p.innerHTML;
  }
  ,
  writeValue : function(/*HTHMLDomElement*/el, /*Object*/value) {
    if (typeof value=='undefined' || value === null) {
      // nothing 
    } 
	else if (mb.lang.isDOMNode(value)) {
      if (value.ownerDocument === el.ownerDocument) {
        if (value.parentNode) {
          value = value.cloneNode(true);
        }
        el.appendChild(value);
      } 
	  else {
        el.innerHTML = mb.dom.toHTML(value);
      }
    } 
	else if (value instanceof Renderer) {
      value.render(el);
    } 
	else if (mb.lang.isArrayLike(value)) {
      jQuery.each(value, function(i, v) {
        mb.dom.writeValue(el, v);
      });
    } 
	else {
      el.appendChild(document.createTextNode(mb.lang.toString(value)));
    }
  }
};
